package com.doucome.snapshot.view
{
	import com.adobe.net.MimeTypeMap;
	import com.doucome.snapshot.Global;
	import fl.controls.Slider;
	import fl.events.SliderEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.adobe.images.JPGEncoder;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	
	/**
	 * ...
	 * @author wkl
	 */
	public class UploadView extends MovieClip
	{
		private var _btn_upload:SimpleButton;
		private var _btn_confirm:SimpleButton;
		private var _btn_reupload:SimpleButton;
		private var fileRef:FileReference;
		private var loader:Loader;
		private var scal:Number;
		//private var bitmap:BitmapData;
		private var image:Bitmap;
		private var _photo_view:MovieClip;
		private var _pic:MovieClip;
		private const HEAD_WIDTH:Number = 210;
		private const HEAD_HEIGHT:Number = 280;
		private var _pic_scroll:Slider;
		private var old_w:Number;
		private var old_h:Number;
		private var _photoBmd:BitmapData;
		private var _error_mask:Sprite;
		private var _no_paper:MovieClip;
		private var _paper:int = 0;
		private var _edit:Boolean = false; //是否为修改状态
		public static const UPLOAD_VIEW_FINISHED_EVENT:String = "UploadViewFinished";
		
		public function UploadView()
		{
			_btn_upload = getChildByName("btn_upload") as SimpleButton;
			_btn_confirm = getChildByName("btn_confirm") as SimpleButton;
			_btn_reupload = getChildByName("btn_reupload") as SimpleButton;
			_photo_view = getChildByName("photo_view") as MovieClip;
			_pic_scroll = getChildByName("pic_scroll") as Slider;
			_no_paper = getChildByName("no_paper") as MovieClip;
			_btn_upload.visible = true;
			removeChild(_no_paper);
			fileRef = new FileReference();
			loader = new Loader();
			_btn_upload.addEventListener(MouseEvent.CLICK, onUploadStart);
			_pic = new MovieClip();
			var _mask:Sprite = new Sprite();
			_mask.graphics.beginFill(0x000000, 100);
			_mask.graphics.drawRect(0, 0, HEAD_WIDTH, HEAD_HEIGHT);
			_pic.mask = _mask;
			_photo_view.addChild(_mask);
			_photo_view.addChild(_pic);
			_pic.buttonMode = true;
			_pic.addEventListener(MouseEvent.MOUSE_DOWN, drag_pic);
			_pic_scroll.addEventListener(SliderEvent.THUMB_DRAG, scroll_change);
			_btn_confirm.addEventListener(MouseEvent.CLICK, onConfirm);
			_btn_reupload.addEventListener(MouseEvent.CLICK, onUploadStart);
			
			_error_mask = new Sprite();
			_error_mask.graphics.beginFill(0x000000, 100);
			_error_mask.graphics.drawRect(0, 0, 760, 580);
			_error_mask.alpha = 0.8;
			_error_mask.visible = false;
		
		}
		
		private function onConfirm(e:MouseEvent):void
		{
			//trace("ok");
			
			var bdm:BitmapData = new BitmapData(HEAD_WIDTH, HEAD_HEIGHT, true);
			bdm.draw(_photo_view);
			_photoBmd = bdm;
			_btn_confirm.visible = false;
			_btn_reupload.visible = false;
			dispatchEvent(new Event(UPLOAD_VIEW_FINISHED_EVENT));
		}
		
		private function drag_pic(e:MouseEvent)
		{
			e.target.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_UP, drag_pic_end);
		}
		
		private function drag_pic_end(e:MouseEvent)
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, drag_pic_end);
			stopDrag();
		}
		
		private function scroll_change(e:SliderEvent):void
		{
			_pic.scaleX = _pic.scaleY = e.target.value / 100;
			_pic.x = _pic.x - (_pic.width - old_w) / 2;
			_pic.y = _pic.y - (_pic.height - old_h) / 2;
			old_w = _pic.width;
			old_h = _pic.height;
			//scaltxt.text = scroll_.value + "%";
		}
		
		private function onUploadStart(e:MouseEvent):void
		{
			fileRef.browse([new FileFilter("*.jpg;*.gif;*.png", "*.jpg;*.gif;*.png")]);
			fileRef.addEventListener(Event.SELECT, onFileSelected);
		}
		
		private function onFileSelected(e:Event):void
		{
			fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
			fileRef.load();
		}
		
		private function onFileLoaded(e:Event):void
		{
			
			loader.loadBytes(e.target.data);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, pic_loaded);
		}
		
		private function pic_loaded(e:Event)
		{
			
			if (null != image && _pic.contains(image))
			{
				_pic.removeChild(image);
				image = null;
			}
			var loadedImage:Bitmap = Bitmap(loader.content);
			var bitmap:BitmapData = new BitmapData(loadedImage.width, loadedImage.height);
			bitmap.draw(loadedImage);
			//image = new Bitmap(getZoomDraw(loadedImage, 210,280,false));
			image = new Bitmap(bitmap, "auto", true);
			_pic.addChild(image);
			pic_init();
			if (contains(_btn_upload))
			{
				removeChild(_btn_upload);
			}
			_btn_confirm.visible = true;
			_btn_reupload.visible = true;
		
		}
		
		private function pic_init():void
		{
			_pic.scaleX = _pic.scaleY = 1;
			if (_pic.height < HEAD_HEIGHT && _pic.width < HEAD_WIDTH)
			{
				scal = 1;
			}
			else
			{
				if (HEAD_WIDTH > HEAD_HEIGHT)
				{
					if (_pic.width > _pic.height)
					{
						_pic.height = _pic.height * (HEAD_WIDTH / _pic.width);
						scal = HEAD_WIDTH / _pic.width;
						_pic.width = HEAD_WIDTH;
							//trace("a:1");
					}
					else
					{
						_pic.width = _pic.width * (HEAD_HEIGHT / _pic.height);
						scal = HEAD_HEIGHT / _pic.height;
						_pic.height = HEAD_HEIGHT;
							//trace("a:2");
					}
				}
				else
				{
					if (_pic.height > _pic.width)
					{
						_pic.width = _pic.width * (HEAD_HEIGHT / _pic.height);
						scal = HEAD_HEIGHT / _pic.height;
						_pic.height = HEAD_HEIGHT;
							//trace("a:3");
					}
					else
					{
						_pic.height = _pic.height * (HEAD_WIDTH / _pic.width);
						scal = HEAD_WIDTH / _pic.width;
						//trace("scal:" + scal + "h:" + _pic.height + "w:" + _pic.width);
						_pic.width = HEAD_WIDTH;
							//trace("a:4" + "h:" + _pic.height + "w:" + _pic.width);
					}
				}
			}
			_pic.x = HEAD_WIDTH / 2 - _pic.width / 2;
			_pic.y = HEAD_HEIGHT / 2 - _pic.height / 2;
			old_w = _pic.width;
			old_h = _pic.height;
			//trace("x:" + _pic.x + "," + "y:" + _pic.y + "," + scal);
			_pic_scroll.value = scal * 100;
		
		}
		
		public function reset():void
		{
			//检查相纸
			if (_paper <= 0)
			{
				addChild(_error_mask);
				_error_mask.visible = true;
				addChild(_no_paper);
				_no_paper.visible = true;
				_no_paper.play();
				_btn_confirm.visible = false;
				_btn_reupload.visible = false;
				_btn_upload.visible = false;
				
			}
			else
			{
				if (contains(_error_mask))
				{
					removeChild(_error_mask);
				}
				if (contains(_no_paper))
				{
					removeChild(_no_paper);
				}
			}
			
			if (null != _photoBmd)
			{
				_photoBmd.dispose();
			}
			
			if (edit)
			{
				//修改
				_btn_upload.visible = false;
				_btn_confirm.visible = true;
				_btn_reupload.visible = true;
			}
			else
			{
				//新上传
				if (null != image && _pic.contains(image))
				{
					_pic.removeChild(image);
					image = null;
				}
				if (!contains(_btn_upload)) { addChild(_btn_upload); _btn_upload.visible = true; }
				_btn_confirm.visible = false;
				_btn_reupload.visible = false;
			}
		
		}
		
		public function get photoBmd():BitmapData
		{
			return _photoBmd;
		}
		
		public function set photoBmd(value:BitmapData):void
		{
			_photoBmd = value;
		}
		
		public function get paper():int
		{
			return _paper;
		}
		
		public function set paper(value:int):void
		{
			_paper = value;
		}
		
		public function get edit():Boolean
		{
			return _edit;
		}
		
		public function set edit(value:Boolean):void
		{
			_edit = value;
		}
	
	}

}